<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/6/14
 * Time: 15:48
 */

namespace App\Rep;


use App\Models\CheckLog;
use App\Models\FinishProduct;
use Illuminate\Support\Facades\DB;

class CheckLogRep
{

    public function getCheckLogListRep($params){
        $model = FinishProduct::Search($params)->where('is_stock_up',2)
            ->with(['order'=>function($q){
                $q->with('check_log');
            },'stock_allocation_log','warehouse','goods_shelf','order_logistics','image1'=>function($q){
                return $q->select('id','file_name','file_url');
            },'image2'=>function($q){
                return $q->select('id','file_name','file_url');
            },'image3'=>function($q){
                return $q->select('id','file_name','file_url');
            },'image4'=>function($q){
                return $q->select('id','file_name','file_url');
            }]);
        $list = $model
            ->where('logistics_status','<>',3)
            ->orderByRaw("FIELD(logistics_status, 1, 4, 2, 5,3)")
            //->toSql();
            ->paginate($params['per_page']??10);
        return $list;
    }


    /**
     * 批量设置已发货
     * @param $params
     */
    public function batchSettingYifahuoRep($params){
        $order_ids = $params['order_ids'];
        DB::beginTransaction();
        try{
            foreach($order_ids as $k=>$v){
                $finish_product = FinishProduct::where('order_id',$v)->first();
                $finish_product->logistics_status = 3;
                $finish_product->to_be_sent_num = 0;
                $finish_product->sent_num = $finish_product->num;
                $finish_product->save();
                //添加发货记录
                $remark = "盘点列表批量设置已发货";
                $orderLogisticsRep = new OrderLogisticsRep();
                $orderLogisticsRep->addOrderLogistics($finish_product->id,$finish_product->order_id,4,$finish_product->num,$remark);
            }
            DB::commit();
            return ['code'=>0,'data'=>[],'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>1001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }
}